package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.dto.ShoppingCartSubDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.BaseException;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.ShoppingCartVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper cartMapper;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void addCart(ShoppingCartDTO dto) {
        Long dishId = dto.getDishId();
        Long setmealId = dto.getSetmealId();
        Long userId = BaseContext.getCurrentId();
        // 判断是否是首次添加本商品
        List<ShoppingCart> shoppingCarts = cartMapper.selectByDishIdOrSetmealId(userId, dishId, setmealId, dto.getDishFlavor());
        log.info("shoppingCarts：{}", shoppingCarts);
        // 是，insert
        if(CollectionUtils.isEmpty(shoppingCarts)){
            // insert
            ShoppingCart shoppingCart = new ShoppingCart();
            // 添加的是菜品
            if(dishId != null && dishId > 0){
                // 查询菜品
                DishVO dish = dishService.getById(dishId);
                if(dish == null){
                    log.error("查询不到菜品：{}", dishId);
                    throw new BaseException(MessageConstant.DISH_NOT_EXIST);
                }
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                // 查询套餐
                Setmeal setmeal = setmealService.getById(setmealId);
                if(setmeal == null){
                    log.error("查询不到套餐：{}", setmealId);
                    throw new BaseException(MessageConstant.SETMEAL_NOT_EXIST);
                }
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setUserId(userId);
            shoppingCart.setDishId(dto.getDishId());
            shoppingCart.setSetmealId(dto.getSetmealId());
            shoppingCart.setDishFlavor(dto.getDishFlavor());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            cartMapper.insert(shoppingCart);
        }else{
            // 否，数量+1， update
            ShoppingCart shoppingCart = shoppingCarts.get(0);
            ShoppingCart cart = new ShoppingCart();
            cart.setId(shoppingCart.getId());
            cart.setNumber(shoppingCart.getNumber() + 1);
            cartMapper.update(cart);
        }
    }

    @Override
    public List<ShoppingCartVO> list() {
        // 获取当前的登录用户
        Long userId = BaseContext.getCurrentId();
        // 查询数据库
        List<ShoppingCart> list = cartMapper.selectByDishIdOrSetmealId(userId, null, null, null);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        // stream
        return list.stream().map(cart->{
            ShoppingCartVO vo= new ShoppingCartVO();
            BeanUtils.copyProperties(cart, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void clean() {
        // 获取当前的登录用户
        Long userId = BaseContext.getCurrentId();
        // 删除
        cartMapper.deleteByUserId(userId);
    }

    @Override
    public void sub(ShoppingCartSubDTO dto) {
        Long userId = BaseContext.getCurrentId();
        Long dishId = dto.getDishId();
        Long setmealId = dto.getSetmealId();
        List<ShoppingCart> list = cartMapper.selectByDishIdOrSetmealIdOrFlavor(userId, dishId, setmealId, dto.getDishFlavor());
        if(CollectionUtils.isEmpty(list)){
            throw new BaseException(MessageConstant.SHOPPING_CART_IS_NULL);
        }
        ShoppingCart shoppingCart = list.get(0);
        Integer number = shoppingCart.getNumber();
        if(number >= 2){
            // update
            ShoppingCart cart = new ShoppingCart();
            cart.setId(shoppingCart.getId());
            cart.setNumber(shoppingCart.getNumber()-1);
            cartMapper.update(cart);
        }else{
            //delete
            cartMapper.deleteByDishIdOrSetmealIdOrFlavor(userId, dishId, setmealId, dto.getDishFlavor());
        }
    }
}
